package me.cxlr.qinlauncher2.util;

import android.content.Context;
import android.content.SharedPreferences;

import me.cxlr.qinlauncher2.MyApplication;

/**
 * @author 酷安用户:山轮纸
 */
public class SharedPreferencesUtil {

    private final static String FILES_NAME = "me.cxlr.qinlauncher2_preferences";

    private static SharedPreferencesUtil sharedPreferencesUtil;

    private SharedPreferences sharedPreferences;

    private SharedPreferencesUtil() {
    }

    public static SharedPreferencesUtil getInstance() {
        if (sharedPreferencesUtil == null) {
            sharedPreferencesUtil = new SharedPreferencesUtil();
        }
        return sharedPreferencesUtil;
    }

    public String getString(String key, String defValue) {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILES_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, defValue);
    }

    public void setString(String key, String value) {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public boolean getBoolean(String key, boolean defValue) {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILES_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defValue);
    }

    public void setBoolean(String key, boolean value) {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public int getInt(String key, int defValue) {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILES_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key, defValue);
    }

    public void setInt(String key, int value) {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public float getFloat(String key, float defValue) {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILES_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getFloat(key, defValue);
    }

    public void setFloat(String key, float value) {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat(key, value);
        editor.apply();
    }
}
